#define _CRT_SECURE_NO_WARNINGS  //scanf()的报错4996,必须放在最开头
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<time.h>

//sscanf: https://blog.csdn.net/weixin_40532377/article/details/86515053

int main(int argc, char* argv[]) {
	srand((size_t)time(NULL)); //随机数种子
	
	int a, b;
	a = rand() % 10 + 5; // 5 ~ 15 之间的随机数
	b = rand() % 10 + 5; // 5 ~ 15 之间的随机数

	printf("a = %d\n", a);
	printf("b = %d\n", b);
	printf("sum = %d\n", a + b);


	int num1, num2, num3;
	char signal;
	char* euqation = "10+5=\n";
	char* save1 = (char*)malloc(sizeof(char) * 64);

	sscanf(euqation, "%d%c%d=\n", &num1, &signal, &num2);  //把字符串拆解成"%d%c%d\n"的格式，并赋值给num1, signal, num2, num3这些变量

	switch (signal) // 根据运算符号进行计算
	{
	case '+':num3 = num1 + num2; break;
	case '-':num3 = num1 - num2; break;
	case '*':num3 = num1 * num2; break;
	case '/':num3 = num1 / num2; break;
	default:
		break;
	}
	printf("%d\n", num3);

	//格式化运算后的字符串并存储到save1中
	sprintf(save1, "%d%c%d=%d\n", num1, signal, num2, num3);
	puts(save1);//自带多一个换行符

	free(save1);
	system("pause");
	return 0;
}

